package com.jxmcloud.business.cut.controller.app.activity;

import cn.hutool.core.util.ObjectUtil;
import com.jxmcloud.business.cut.constants.CutErrorMsg;
import com.xlcloud.platform.api.exception.BusinessException;
import com.xlcloud.platform.util.RUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import com.jxmcloud.business.cut.service.user.IUserManagerService;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.util.RedissonLockUtil;
import com.jxmcloud.business.cut.constants.CutRedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.redisson.api.RedissonClient;
import com.jxmcloud.business.cut.service.user.IUserActivityService;


import com.jxmcloud.business.cut.service.app.activity.IInviteSuccessService;
import com.jxmcloud.business.cut.dto.client.FreshReceivedInviteDto;
import com.jxmcloud.business.cut.vo.client.FreshReceivedInviteVo;
import com.jxmcloud.business.cut.dto.client.GainDrawValueAwardDto;
import com.jxmcloud.business.cut.vo.client.GainDrawValueAwardVo;
import com.jxmcloud.business.cut.dto.client.GetInviteAwardDto;
import com.jxmcloud.business.cut.vo.client.GetInviteAwardVo;
import com.jxmcloud.business.cut.dto.client.GetInviteAwardProgressDto;
import com.jxmcloud.business.cut.vo.client.GetInviteAwardProgressVo;
import com.jxmcloud.business.cut.dto.client.GetInviteShareUrlDto;
import com.jxmcloud.business.cut.vo.client.GetInviteShareUrlVo;
import com.jxmcloud.business.cut.dto.client.InvitedNewersListDto;
import com.jxmcloud.business.cut.vo.client.InvitedNewersListVo;
import com.jxmcloud.business.cut.dto.client.ShareSuccessDto;
import com.jxmcloud.business.cut.vo.client.ShareSuccessVo;


@Validated
@Slf4j
@RestController
@RequestMapping("/app/activity/InviteSuccessController")
public class InviteSuccessController {
    @Resource
    private IUserManagerService userManagerService;
    @Resource
    private IUserActivityService userActivityService;
    @Autowired
    private RedissonClient redissonClient;
    @Resource
    private IInviteSuccessService inviteSuccessService;


    // 通知服务器 刷新邀请信息
    @PostMapping("/freshReceivedInvite")
    public String freshReceivedInvite(@RequestBody @Valid FreshReceivedInviteDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            FreshReceivedInviteVo data = inviteSuccessService.freshReceivedInvite(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }

    // 领取提现值(当前所有可领取的总的奖励)
    @PostMapping("/gainDrawValueAward")
    public String gainDrawValueAward(@RequestBody @Valid GainDrawValueAwardDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            GainDrawValueAwardVo data = inviteSuccessService.gainDrawValueAward(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }

    // 领取邀请成功的奖励
    @PostMapping("/getInviteAward")
    public String getInviteAward(@RequestBody @Valid GetInviteAwardDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            GetInviteAwardVo data = inviteSuccessService.getInviteAward(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }

    // 领取走分享流程的奖励
    @PostMapping("/getInviteAwardProgress")
    public String getInviteAwardProgress(@RequestBody @Valid GetInviteAwardProgressDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            GetInviteAwardProgressVo data = inviteSuccessService.getInviteAwardProgress(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }

    // 获取邀请的分享地址
    @PostMapping("/getInviteShareUrl")
    public String getInviteShareUrl(@RequestBody @Valid GetInviteShareUrlDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            GetInviteShareUrlVo data = inviteSuccessService.getInviteShareUrl(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }

    // 已成功邀请的新人列表
    @PostMapping("/invitedNewersList")
    public String invitedNewersList(@RequestBody @Valid InvitedNewersListDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            InvitedNewersListVo data = inviteSuccessService.invitedNewersList(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }

    // 分享成功
    @PostMapping("/shareSuccess")
    public String shareSuccess(@RequestBody @Valid ShareSuccessDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            ShareSuccessVo data = inviteSuccessService.shareSuccess(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }
}
